package com.youlu.campus.web.rd;

import com.youlu.campus.entity.VO.req.ActivityJoinInfoRequest;
import com.youlu.campus.entity.VO.req.ActivityJoinNumReq;
import com.youlu.campus.entity.VO.req.ActivityRankFlushRequest;
import com.youlu.campus.service.course.UserActivityService;
import com.youlu.campus.service.rank.RankBaseService;
import com.youlu.campus.service.rank.RankService;
import com.youlu.campus.service.ranking.UserActivityPointRankingService;
import com.youlu.campus.service.rd.RdService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 面向研发Controller 清理数据
 */
@Slf4j
@RestController
public class RdDataController {
    @Autowired
    private RankService rankService;
    @Autowired
    private RankBaseService rankBaseService;
    @Autowired
    private UserActivityPointRankingService userActivityPointRankingService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private RdService rdService;
    @Autowired
    private UserActivityService userActivityService;

    @PostMapping("/public/rd/rank-cache")
    public QResult flushRankList(@RequestBody ActivityRankFlushRequest rankFlushRequest) {
        log.info(":>>> 开始刷新活动:{} 全国榜和省榜");
        rankService.flushUserRankListToCanche(rankFlushRequest);
        return QResult.success("OK");
    }

    @GetMapping("/mini-app/rd/rank-order")
    public QResult getRankOrder(@RequestParam(value = "activityId", required = false) String activityId,
                                @RequestParam(value = "userId", required = false) String userId) {
        log.info(":>>> 开始获取活动:{} 全国榜和省榜，名次");
        String key = String.format(userActivityPointRankingService.getRankPreKey(), activityId);
        Long order = rankBaseService.getSingleRankOrder(key, userId);
        return QResult.success(order);
    }

    @GetMapping("/mini-app/rd/rank-delete-one")
    public QResult deleteOneRank(@RequestParam(value = "activityId", required = false) String activityId,
                                 @RequestParam(value = "userId", required = false) String userId,
                                 @RequestParam(value = "pw", required = false) String pw) {
        if (!"gqgood.com".equals(pw)) {
            return QResult.fail("密码错误");
        }
        log.info(":>>> 开始删除活动:{} 全国榜和省榜");
        rdService.deleteSingleRank(userId, activityId);
        return QResult.success(true);
    }

    @GetMapping("/mini-app/rd/rank-delete")
    public QResult deleteAllRank(@RequestParam(value = "activityId", required = false) String activityId,
                                 @RequestParam(value = "userId", required = false) String userId,
                                 @RequestParam(value = "pw", required = false) String pw) {
        if (!"gqgood.com".equals(pw)) {
            return QResult.fail("密码错误");
        }
        log.info(":>>> 开始删除活动:{} 全国榜和省榜", activityId);
        rdService.deleteRank(userId, activityId);
        return QResult.success(true);
    }

    @PostMapping("/mini-app/rd/sync/join/info")
    public QResult getRankOrder(@RequestBody ActivityJoinInfoRequest request) {
        rdService.syncJoinInfo(request);
        return QResult.success(true);
    }

    @PostMapping("/mini-app/update/wechat/gray/logo")
    public QResult updateWechatGrayLogo() {
        rdService.updateWechatGrayLogo();
        return QResult.success(true);
    }

    @GetMapping("/public/activity/join-num/delete")
    public QResult deleteJoinNum(@RequestParam("activityId") String activityId) {
        userActivityService.deleteActivityRandomNum(activityId);
        return QResult.success(true);
    }

    @PostMapping("/public/activity/join-num/set")
    public QResult setJoinNum(@RequestBody @NotNull ActivityJoinNumReq re) {
        userActivityService.setActivityRandomNum(re.getActivityId(), re.getNum());
        return QResult.success(true);
    }
}
